package nongchang.kuaijiecaifu.com.ncapp_android.ui;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ezy.lite.util.Param;
import ezy.widget.adapter.TabsAdapter;
import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.common.activity.BaseActivity;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.event.MutipleMoneyEvent;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.fragment.basketdetailfragment.BasketGoodDetailFragment;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.popup.SureOrderDialog;
import nongchang.kuaijiecaifu.com.ncapp_android.util.RxBus;

public class BasketDetailActivity extends BaseActivity {
    @BindView(R.id.orderby)
    TabLayout mTabOrderBy;
    @BindView(R.id.pager)
    ViewPager mViewPager;
    @BindView(R.id.ll_type)
    LinearLayout llType;
    @BindView(R.id.edit_simple_total)
    EditText edit_simple_total;
    @BindView(R.id.txt_money)
    TextView txtMoney;
    private float money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basket_detail);
        ButterKnife.bind(this);
        Fragment fragments[] = {new BasketGoodDetailFragment(), new BasketGoodDetailFragment(), new BasketGoodDetailFragment()};
        mViewPager.setAdapter(new TabsAdapter(getSupportFragmentManager(), fragments));
        mTabOrderBy.addTab(mTabOrderBy.newTab().setText("商品详情"), true);
        mTabOrderBy.addTab(mTabOrderBy.newTab().setText("售后服务"));
        mTabOrderBy.addTab(mTabOrderBy.newTab().setText("晒单记录"));
        mTabOrderBy.setOnTabSelectedListener(onTabSelectedListener);
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabOrderBy));


        int type =getIntent().getIntExtra("hotType",1);
        money=getIntent().getFloatExtra("money",0);
//        if (type == 1) {
//            llType.setVisibility(View.VISIBLE);
            txtMoney.setText("总计：￥"+money);
//        } else {
//            llType.setVisibility(View.GONE);
//        }
    }

    TabLayout.OnTabSelectedListener onTabSelectedListener = new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            mViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    };
    private int allCount = 1;

    @OnClick({R.id.btn_back, R.id.btn_add_cart, R.id.btn_simple_sub, R.id.btn_simple_add})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_add_cart:
                new SureOrderDialog(this).show();
                break;
            case R.id.btn_simple_sub:
                if (getNum(edit_simple_total) <= 1) {
                    return;
                }
                allCount--;
                setNum(edit_simple_total, allCount);
                txtMoney.setText("总计：￥"+money*allCount);
                break;
            case R.id.btn_simple_add:
                allCount++;
                setNum(edit_simple_total, allCount);
                txtMoney.setText("总计：￥"+money*allCount);
                break;
        }
    }

    private int getNum(EditText num) {
        return Integer.valueOf(num.getText().toString());
    }

    private void setNum(EditText EditTextnum, int number) {
        EditTextnum.setText(number + "");
    }
}
